using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data.SqlClient;
using System.Data;
using Microsoft.ApplicationBlocks.Data;
using System.IO;
using Microsoft.SqlServer.Dts.Runtime;
using System.Xml;

namespace Microsoft.SharedSource.SqlServer.MDDE.Repository
{
    public sealed class TemplateScalarProperty
    {
        #region Fields

        TemplateConfigurableComponent parentComponent;
        string name;
        string type;

        #endregion

        #region Properties

        public TemplateConfigurableComponent ParentComponent
        {
            get { return parentComponent; }
        }

        public string PropertyName
        {
            get { return name; }
        }

        public string Type
        {
            get { return type; }
            set { type = value; }
        }

        #endregion

        #region Methods

        public TemplateScalarProperty(
            TemplateConfigurableComponent parentComponent,
            string propertyName)
            : this(parentComponent, propertyName, null)
        { }

        public TemplateScalarProperty(
            TemplateConfigurableComponent parentComponent,
            string propertyName, SqlTransaction tn)
        {
            this.parentComponent = parentComponent;
            this.name = propertyName;

            // See if we can get this property from repository.
            SqlParameter[] sqlParams = new SqlParameter[2];
            sqlParams[0] = new SqlParameter("componentId", parentComponent.Id);
            sqlParams[1] = new SqlParameter("propertyName", propertyName);

            DataSet propertyInfo;
            if (tn == null)
                propertyInfo = SqlHelper.ExecuteDataset(
                parentComponent.ParentTemplate.
                ParentRepository.RepositoryConnection,
                CommandType.Text,
                Resource.GetTemplateScalarPropertyInfo, sqlParams);
            else
                propertyInfo = SqlHelper.ExecuteDataset(tn,
                CommandType.Text,
                Resource.GetTemplateScalarPropertyInfo, sqlParams);

            // We could not find the property.
            if (propertyInfo.Tables[0].Rows.Count == 0)
                return;

            // We could find the property details.
            DataRow r = propertyInfo.Tables[0].Rows[0];
            this.type = r["PropertyType"].ToString();
        }

        public void Delete(SqlTransaction tn)
        {
            // Delete template scalar property.
            SqlParameter[] spParams = new SqlParameter[2];
            spParams[0] = new SqlParameter("@componentId",
                this.ParentComponent.Id);
            spParams[1] = new SqlParameter("@propertyName",
                this.PropertyName);

            SqlHelper.ExecuteScalar(tn,
                CommandType.StoredProcedure,
                "sp_DeleteTemplateScalarProperty", spParams);
        }

        internal void Create(SqlTransaction tn)
        {
            SqlParameter[] spParams = new SqlParameter[3];
            spParams[0] = new SqlParameter("@componentId",
                this.ParentComponent.Id);
            spParams[1] = new SqlParameter("@propertyName",
                this.PropertyName);
            spParams[2] = new SqlParameter("@propertyType",
                this.Type);

            // Create a new component scalar property.
            Convert.ToInt32(
                SqlHelper.ExecuteScalar(tn,
                "sp_CreateTemplateScalarProperty", spParams));
        }

        #endregion
    }

    public sealed class TemplateScalarPropertyCollection : IEnumerable
    {
        #region Fields

        private Dictionary<string, TemplateScalarProperty>
            scalarProperties =
            new Dictionary<string, TemplateScalarProperty>();

        TemplateConfigurableComponent parentComponent;

        #endregion

        #region Properties

        public TemplateConfigurableComponent ParentComponent
        {
            get { return parentComponent; }
        }

        public TemplateScalarProperty this[string propertyName]
        {
            get { return scalarProperties[propertyName]; }
        }

        #endregion

        #region Methods

        internal TemplateScalarPropertyCollection(
            TemplateConfigurableComponent parentComponent)
        {
            this.parentComponent = parentComponent;
        }

        public void Add(TemplateScalarProperty scalarProperty)
        {
            scalarProperties.Add(scalarProperty.PropertyName, scalarProperty);
        }

        public void Remove(TemplateScalarProperty scalarProperty)
        {
            scalarProperties.Remove(scalarProperty.PropertyName);
        }

        public void Clear()
        {
            scalarProperties.Clear();
        }

        public bool ContainsScalarProperty(string propertyName)
        {
            return scalarProperties.ContainsKey(propertyName);
        }

        #endregion

        #region IEnumerable Members

        public IEnumerator GetEnumerator()
        {
            return scalarProperties.Values.GetEnumerator();
        }

        #endregion
    }
}
